package com.xclink.common.controller;

import java.beans.PropertyEditorSupport;
import java.math.BigDecimal;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import com.xclink.common.utils.BaseLogger;
import com.xclink.common.utils.DateTypeEditor;

/**
 * 基础的Controller,所有的Controller必须继承此类
 *
 * @author yxt
 */
public class BaseController<T> extends BaseLogger {	

    /**
     * 初始化映射格式.
     *
     * @param binder
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new DateTypeEditor());
        binder.registerCustomEditor(Integer.class, new PropertyEditorSupport() {
            public void setAsText(String value) {
                try {
                    if (StringUtils.isNotBlank(value)) {
                        setValue(Integer.valueOf(value));
                    } else {
                        setValue(null);
                    }
                } catch (Exception e) {
                    setValue(null);
                    logger.error(e.getMessage(), e);
                }
            }
        });
        binder.registerCustomEditor(Long.class, new PropertyEditorSupport() {
            public void setAsText(String value) {
                try {
                    if (StringUtils.isNotBlank(value)) {
                        setValue(Long.valueOf(value));
                    } else {
                        setValue(null);
                    }
                } catch (Exception e) {
                    setValue(null);
                    logger.error(e.getMessage(), e);
                }
            }
        });

        binder.registerCustomEditor(Double.class, new PropertyEditorSupport() {
            public void setAsText(String value) {
                try {
                    if (StringUtils.isNotBlank(value)) {
                        setValue(Double.valueOf(value));
                    } else {
                        setValue(null);
                    }
                } catch (Exception e) {
                    setValue(null);
                    logger.error(e.getMessage(), e);
                }
            }
        });

        binder.registerCustomEditor(BigDecimal.class,
                new PropertyEditorSupport() {
                    public void setAsText(String value) {
                        try {
                            if (StringUtils.isNotBlank(value)) {
                                setValue(new BigDecimal(value));
                            } else {
                                setValue(null);
                            }
                        } catch (Exception e) {
                            setValue(null);
                            logger.error(e.getMessage(), e);
                        }
                    }
                });

    }
}
